package inherit.practice.demo02.ArrayList04;

public class TestSelect {
    public static void main(String[] args) {
        // 查询id是否存在，如果存在年龄加一
        Student[] arr = new Student[3];
        Student stu1 = new Student(1,"小平",18);
        Student stu2 = new Student(2,"小同",19);
        Student stu3 = new Student(3,"小神",20);
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        int index = getIndex(arr,4);
        if(index>=0){
            Student stu = arr[index];
            int newAge = stu.getAge()+1;
            stu.setAge(newAge);
            printArr(arr);
        }else {
            System.out.println("当前id不存在");
        }

    }
    public static int getIndex(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                if(stu.getId() == id){
                    return i;
                }
            }
        }
        return -1;
    }

    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu !=null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }

        }
    }
}
